<template>
  <div>
    <danger-detail-info :form="form"></danger-detail-info>
    <div class="block-box" >
      <div class="block-box_item">
        <div class="block-box_title">制定措施</div>
        <eagle-radio-group
          name="radio"
          label="判定"
          required
          v-model="radio"
          direction="vertical"
        >
          <eagle-radio icon-size="18px" name="1">制定措施</eagle-radio>
          <eagle-radio icon-size="18px" name="2">退回隐患</eagle-radio>
          <eagle-radio icon-size="18px" name="3" v-if="isSimple">重新指派</eagle-radio>
        </eagle-radio-group>
        <div v-if="radio == 3">
          <van-field
            class="input_text"
            :value="editForm.CorrectiveChnName"
            label="整改人"
            placeholder="请选择"
            required
            readonly
            @click="showDeptUser()"
          >
            <template #right-icon><van-icon name="arrow" /></template>
          </van-field>
        </div>
        <div v-if="radio == 2 || radio == 3">
          <eagle-input
            v-model="remarks"
            rows="2"
            required
            autosize
            label="说明"
            type="textarea"
            placeholder="请输入说明"
            isInputText
          ></eagle-input>
        </div>
        <div v-if="radio == 1">
          <eagle-switch name="reason" label="原因分析" v-model="reason" size="20"></eagle-switch>
          <div v-if="reason">
            <eagle-input 
              v-model="editForm.Analysis"
              rows="3"
              autosize
              label=""
              type="textarea"
              placeholder="请输入原因分析"
              isInputText
            ></eagle-input>
          </div>

          <eagle-radio-group
            name="radio"
            label="整改方式"
            v-model="editForm.CorrectiveMethod"
            direction="horizontal"
            required
          >
            <eagle-radio icon-size="18px" name="1">立即整改</eagle-radio>
            <eagle-radio icon-size="18px" name="2">限期整改</eagle-radio>
          </eagle-radio-group>

          <eagle-date
            label="整改期限"
            required
            v-if="editForm.CorrectiveMethod == '2'"
            :date.sync="editForm.CorrectiveDate"
          ></eagle-date>

          <eagle-input
            v-if="editForm.CorrectiveMethod == '2'"
            v-model="editForm.CorrectiveMeasure"
            required
            rows="3"
            autosize
            label="整改措施"
            type="textarea"
            placeholder="请输入整改措施"
            isInputText
          ></eagle-input>

          <eagle-input
            name="uploader"
            label="整改图片"
            v-if="editForm.CorrectiveMethod == '1'"
            required
          >
            <template v-if="editForm.CorrectiveMethod == '1'" #input>
              <eagle-image-uploader :images.sync="editForm.CorrectiveAttachs"></eagle-image-uploader>
            </template>
          </eagle-input>
          <eagle-input
            v-if="editForm.CorrectiveMethod == '1'"
            v-model="editForm.CorrectiveDesc"
            required
            rows="3"
            autosize
            label="整改说明"
            type="textarea"
            placeholder="请输入整改说明"
            isInputText
          ></eagle-input>
          <eagle-switch name="expenses" label="需要经费" v-model="expenses" size="20"></eagle-switch>
          <eagle-input
            v-if="expenses"
            v-model="editForm.Expenses"
            center
            clearable
            placeholder="请输入经费"
            isInputText
          >
            <template #button> 元 </template>
          </eagle-input>
        </div>
      </div>
      <div class="btns mt-10">
        <eagle-button class="submit-only" @click="submit">提交</eagle-button>
      </div>
    </div>
    <user-choose-dialog
      :isMulti="false"
      @callback="onUserCallback"
      :show.sync="showUserChooseDialog"
    ></user-choose-dialog>
  </div>
</template>

<script>
import { getDate, dateAdd } from "@/utils/getDate";
import { deepClone } from "@/utils";
import DangerDetailInfo from "./dangerDetailInfo.vue";
import UserChooseDialog from "@/views/components/user/userChooseDialog.vue";
export default {
  components: { DangerDetailInfo, UserChooseDialog },
  data() {
    return {
      id: this.$route.query.id,
      correctiveDate: null,
      reason: false,
      expenses: false,
      showCalendar: false,
      form: {},
      editForm: {},
      radio: "1",
      isSimple: false,
      showUserChooseDialog: false,
      remarks:'',
    };
  },
  created() {
    this.geDetail();
  },
  methods: {
    // 打开日历
    openCalendar() {
      this.showCalendar = true;
    },
    showDeptUser() {
      this.showUserChooseDialog = true;
    },
    // 选择部门人回调
    onUserCallback(item) {
      this.editForm.CorrectiveUserName = item[0].code;
      this.editForm.CorrectiveChnName = item[0].name;
      this.showUserChooseDialog = false;
    },
    // 获取详情
    geDetail() {
      var me = this;
      this.$axios
      .get("/DangerCheckTaskDetail/GetDetail",{params:{ id: this.id }})
      .then((res)=>{
        me.form = res.data;
        me.editForm = deepClone(res.data);
        me.editForm.CorrectiveDate = getDate(
          me.editForm.CorrectiveDate || dateAdd("d", 7, new Date())
        );
        me.correctiveDate = new Date(me.editForm.CorrectiveDate);
        me.editForm.CorrectiveMethod = me.editForm.CorrectiveMethod || "2";
        me.editForm.CorrectiveMeasure =
          me.editForm.CorrectiveMeasure || me.editForm.CorrectiveAdvise;
        me.editForm.CorrectiveDesc =
          me.editForm.CorrectiveDesc || me.editForm.CorrectiveAdvise;
        if (me.editForm.Analysis != "" && me.editForm.Analysis != null) {
          me.reason = true;
        }
        if (me.editForm.Expenses != "" && me.editForm.Expenses != null) {
          me.expenses = true;
        }
        me.isSimple = res.data.DangerSimpleType == "A";
      })
    },
    onConfirm(date) {
      this.editForm.CorrectiveDate = getDate(date);
      this.correctiveDate = date;
      this.showCalendar = false;
    },

    submit() {
      // this.loading = true
      if (this.radio == 1) {
        let param = {
          ID: this.editForm.ID,
          CorrectiveMethod: this.editForm.CorrectiveMethod,
          CorrectiveDate: this.editForm.CorrectiveDate,
          CorrectiveMeasure: this.editForm.CorrectiveMeasure,
          CorrectiveAttachs: this.editForm.CorrectiveAttachs,
          CorrectiveDesc: this.editForm.CorrectiveDesc,
          Analysis: this.editForm.Analysis,
          Expenses: this.editForm.Expenses,
        };
        if (param.CorrectiveMethod == "1") {
          if (!param.CorrectiveAttachs) {
            this.$toast("请选择整改图片");
            return false;
          }
          if (!param.CorrectiveDesc) {
            this.$toast("请输入整改说明");
            return false;
          }
        } else {
          if (!param.CorrectiveDate) {
            this.$toast("请选择整改日期");
            return false;
          }
          if (!param.CorrectiveMeasure) {
            this.$toast("请输入整改措施");
            return false;
          }
        }
        this.$axios
        .post("/DangerCheckTaskDetail/ConfirmFormulate",param)
        .then((res)=>{
           this.$toast("保存成功");
          this.$router.goRefresh(-1);
        })
        .catch((err) => {
          console.log("err", err);
        });
      } else if (this.radio == 2) {
        let param = {
          ID: this.editForm.ID,
          Remarks: this.remarks,
        };
        if (!param.Remarks) {
          this.$toast("请输入说明");
          return false;
        }
        this.$axios
        .post("/DangerCheckTaskDetail/BackFormulate",param)
        .then((res)=>{
          this.$toast("保存成功");
          this.$router.goRefresh(-1);
        })
        .catch((err) => {
          console.log("err", err);
        });
      } else if (this.radio == 3) {
        this.editForm.Remarks=this.remarks;
        if (!this.editForm.Remarks) {
            this.$toast("请输入说明");
            return false;
          }
          if (!this.editForm.CorrectiveUserName) {
            this.$toast("请输入整改人");
            return false;
          }
        this.$axios
        .post("/DangerCheckTaskDetail/FormulateAgainAssing",this.editForm)
        .then((res)=>{
          this.$toast("保存成功");
          this.$router.goRefresh(-1);
        })
        .catch((err) => {
          console.log("err", err);
        });
      }
    },
  },
};
</script>
